package com.wbs.controller;

import com.alibaba.fastjson.JSONObject;
import com.wbs.model.TbAddress;
import com.wbs.resutil.Result;
import com.wbs.service.AddressService;
import com.wbs.utils.JwtUtil;
import com.wbs.vo.AddressVO;
import com.wbs.vo.UserVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 芝梦
 * @Description: 订单控制层
 * @DateTime: 2024/2/27 15:24
 */
@RequestMapping("address")
@RestController
public class AddressController {
    @Autowired
    private AddressService addressService;

    @Autowired
    private JwtUtil jwtUtil;

    //选择按用户名获取地址
    @GetMapping("selectAddressByUsername")
    public Result selectAddressByUsername(@RequestHeader String token){
        //解析token
        Claims claims = jwtUtil.parseJwt(token);
        String subject = claims.getSubject();
        UserVO userVO = JSONObject.parseObject(subject, UserVO.class);
        List<AddressVO> addressVOS = addressService.selectAddressByUsername(userVO.getUsername());
        return Result.SUCCESS().setData(addressVOS);
    }

    //通过id查询地址信息
    @GetMapping("selectAddrById")
    public TbAddress selectAddrById(@RequestParam Long id){
        return addressService.selectAddrById(id);
    }

}
